package com.sanji.pay.pay.web;

import com.sanji.pay.allinpay.entity.AllinPay;
import com.sanji.pay.allinpay.service.AllinPayService;
import com.sanji.pay.common.Result;
import com.sanji.pay.common.utils.ExcelExport;
import com.sanji.pay.pay.dto.PayQueryModel;
import com.sanji.pay.pay.entity.PayOrder;
import com.sanji.pay.pay.service.PayOrderService;
import com.sanji.pay.pay.vo.PayDealTypeVO;
import com.sanji.pay.pay.vo.PayDetailVO;
import com.sanji.pay.pay.vo.PayStatusVO;
import com.sanji.pay.sys.domain.Account;
import com.sanji.pay.sys.repository.AccountRepository;
import com.sanji.pay.yeepay.entity.YEEPay;
import com.sanji.pay.yeepay.service.YeePayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.sanji.pay.common.ResultGenerator.ok;

/**
 * Created by cbh on 17-9-19.
 */
@RestController
//@RequestMapping(value = "/sys/payOrder", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RequestMapping(value = "/sys/payOrder")
public class PayOrderController {

  @Autowired
  private PayOrderService payOrderService;

  @Autowired
  private AllinPayService allinPayService;

  @Autowired
  private YeePayService yeePayService;

  @Autowired
  private AccountRepository accountRepository;
  /**
   * 订单列表
   * @param cusId
   * @param payQueryModel
   * @param pageable
   * @return
   */
  @GetMapping("/{cusId}")
  public Result<Page<PayOrder>> getAllPayOrder(@PathVariable String cusId,
                                             PayQueryModel payQueryModel,
                                             Pageable pageable){
//    Page<PayOrder> payOrders = payOrderService.findAll(sourceType,startTime,endTime,payNum,orderNum,pageable);
    String startTime = payQueryModel.getStartTime();
    String endTime = payQueryModel.getEndTime();
    String payNum = payQueryModel.getPayNum();
    String orderNum = payQueryModel.getOrderNum();
    String dealType = payQueryModel.getDealType();
    Integer status = payQueryModel.getStatus();
    Page<PayOrder> payOrders = payOrderService.findAll(cusId,startTime,endTime,payNum,orderNum,dealType,status,pageable);
    return ok(payOrders);
  }

  /**
   * 支付订单展开详情
   * @param payQueryModel
   * @return
   */
  @GetMapping
  public Result<List<PayDetailVO>> getPayOrderDetail(PayQueryModel payQueryModel){
    List<PayDetailVO> detailVOS = new ArrayList<>();
    PayDetailVO payDetailVO = new PayDetailVO();
    String dealType = payQueryModel.getDealType();
    switch (dealType){
      case "allinPay":
        AllinPay allinPay = allinPayService.findByPayNum(payQueryModel.getPayNum());
        payDetailVO.setPayTime(allinPay.getPayTime());
        payDetailVO.setTrxId(allinPay.getTrxId());
        detailVOS.add(payDetailVO);
        break;
      case "yeePay":
        YEEPay yeePay = yeePayService.findByPayNum(payQueryModel.getPayNum());
        payDetailVO.setPayTime(yeePay.getPayDate());
        payDetailVO.setTrxId(yeePay.getTrxId());
        detailVOS.add(payDetailVO);
        break;
      default:
        break;
    }
    return ok(detailVOS);
  }

  /**
   * 导出交易记录
   * @param cusId
   * @param payQueryModel
   * @param request
   * @param response
   */
  @GetMapping("/export/{cusId}")
  public void exportPayOrder(@PathVariable String cusId,PayQueryModel payQueryModel,
                             HttpServletRequest request, HttpServletResponse response){
    String startTime = payQueryModel.getStartTime();
    String endTime = payQueryModel.getEndTime();
    List<PayOrder> payOrders = payOrderService.findAllByExport(cusId,startTime,endTime);
    String[] gridTitles_ = { "交易单号","订单编号","支付方式","交易类型", "交易金额","交易状态","交易时间"};
    String[] coloumsKey_ = { "payNum","orderNum","payment","dealType","amount","status","payTime"};
    ExcelExport.doExcelExport("支付记录.xls", payOrders, gridTitles_, coloumsKey_, request, response);
  }

  /**
   * 下拉订单状态列表
   * @return
   */
  @GetMapping("/status")
  public Result<List<PayStatusVO>> getPayStatus(){
    PayOrder.PayStatus[] payStatus = PayOrder.PayStatus.values();
    List<PayStatusVO> payStatusVOS = new ArrayList<>();
    for(PayOrder.PayStatus status : payStatus){
      payStatusVOS.add(new PayStatusVO(status.getIndex(),status.getName()));
    }
    return ok(payStatusVOS);
  }

  /**
   * 下拉订单状态列表
   * @return
   */
  @GetMapping("/{cusId}/dealType")
  public Result<List<PayDealTypeVO>> getDealType(@PathVariable String cusId){
    Account account = accountRepository.findByCusId(cusId);
    String[] payTypes = account.getPayType().split(",");
    List<PayDealTypeVO> payDealTypeVOS = new ArrayList<>();
    for(String payType: payTypes){
      switch (payType){
        case "yeePay":
          payDealTypeVOS.add(new PayDealTypeVO(payType,"网银支付"));
          break;
        case "AliPay":
          payDealTypeVOS.add(new PayDealTypeVO(payType,"支付宝"));
          break;
        case "WeChat":
          payDealTypeVOS.add(new PayDealTypeVO(payType,"微信"));
          break;
        case "wallet":
          payDealTypeVOS.add(new PayDealTypeVO(payType,"钱包"));
          break;
        case "HDF":
          payDealTypeVOS.add(new PayDealTypeVO(payType,"货到付款"));
        default:
          break;
      }
    }
    return ok(payDealTypeVOS);
  }
}
